package com.rentcars.controller;

import com.github.pagehelper.Page;
import com.rentcars.entity.CarInsurance;
import com.rentcars.service.CarInsuranceService;
import com.rentcars.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 赵瑞硕
 * 车辆管理-保险
 */
@RestController
@RequestMapping("/insures")
public class CarInsuranceController {
    @Autowired
    private CarInsuranceService carInsuranceService;
    @RequestMapping(value="/getPages")
    public Map queryCarInsurByPage(int pageNum, int pageSize,String carid){
        Map map=new HashMap();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("carid",carid);
        Page page= carInsuranceService.queryCarInsurByPage(map);
        map.put("page",page);
        map.put("totalPage",page.getPages());
        return  map;
    }
    @RequestMapping(value="/select")
    public ModelAndView toIn(){
        return new ModelAndView("insurance-list");
    }

    @RequestMapping(value="/edit")
    public ModelAndView getIn(String recordid) throws ParseException {
        CarInsurance carInsurance=carInsuranceService.selectByPrimaryKey(recordid);
        Date addtime= DateUtils.getDate(carInsurance.getAddtime()); ;
        Date invaliddate= DateUtils.getDate(carInsurance.getInvaliddate());
        Date validdate= DateUtils.getDate(carInsurance.getValiddate());
        Map<String,Object>rsmap=new HashMap<>();
        rsmap.put("addtime",addtime);
        rsmap.put("invaliddate",invaliddate);
        rsmap.put("validdate",validdate);
        rsmap.put("datas",carInsurance);
        return new ModelAndView("insurance-edit",rsmap);
    }
    @RequestMapping(value="/del")
    public int delIn(String recordid){
        return   carInsuranceService.deleteByPrimaryKey(recordid);
    }
    @RequestMapping(value="/add")
    public ModelAndView aDDin(){
        return new ModelAndView("insurance-add");
    }
    @RequestMapping(value="/addsss")
    public int addIn(CarInsurance carInsurance){
        CarInsurance car=carInsuranceService.selectByPrimaryKey(carInsurance.getRecordid());
        if (car==null){
            return carInsuranceService.insertSelective(carInsurance);
        }else {
            return 0;
        }
    }
    @RequestMapping(value="/updata")
    public int upIn(CarInsurance carInsurance) {
        return carInsuranceService.updateByPrimaryKeySelective(carInsurance);
    }

}
